package com.mmche.app.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.framework.base.util.Tool;
import com.framework.support.domain.PageMan;
import com.mmche.car.dao.CarDao;
import com.mmche.car.dao.impl.CarDaoImpl;
import com.mmche.car.pojo.Car;

@Repository("appCarDao")
public class AppCarDaoImpl extends CarDaoImpl implements CarDao {
	
	@Override
	public List<Car> getPageByMap(Map<String, Object> map, PageMan pageMan) {
		Query query = new Query();
		@SuppressWarnings("unchecked")
		List<String> ids = (List<String>)map.get("ids");
		String carId = (String)map.get("carId");
		String brand = (String)map.get("brand");
		String models = (String)map.get("models");
		Integer status = (Integer)map.get("status");
		String carType = (String)map.get("carType");
		Boolean promo = (Boolean)map.get("promo");
		String startPrice = (String)map.get("startPrice");
		String endPrice = (String)map.get("endPrice");
		String startYear = (String)map.get("startYear");
		String endYear = (String)map.get("endYear");
		String startDate = (String)map.get("startDate");
		String endDate = (String)map.get("endDate");
		String sort = (String)map.get("sort");
		String sortType = (String)map.get("sortType");
		String keywords = (String)map.get("keywords");
		String shelves = (String)map.get("shelves");
		if(!Tool.isEmpty(carId)){
			query.addCriteria(Criteria.where("carId").regex(".*"+carId.trim()+".*"));
		}
		if(!Tool.isEmpty(brand)){
			query.addCriteria(Criteria.where("brand.$id").is(brand.trim()));
		}
		if(!Tool.isEmpty(models)){
			query.addCriteria(Criteria.where("models.$id").is(models.trim()));
		}
		if(!Tool.isEmpty(carType)){
			query.addCriteria(Criteria.where("carType").is(Integer.parseInt(carType.trim())));
		}
		if(startPrice != null || endPrice != null){
			Criteria criteria = Criteria.where("online.price");
			if(startPrice !=null )
				criteria.gte(Float.parseFloat(startPrice)*10000);
			if(endPrice !=null )
				criteria.lte(Float.parseFloat(endPrice)*10000);
			query.addCriteria(criteria);
		}
		if(startDate != null || endDate != null){
			Criteria criteria = Criteria.where("registerTime");
			if(startDate !=null )
				criteria.gte(startDate);
			if(endDate !=null )
				criteria.lte(endDate);
			query.addCriteria(criteria);
		}
		else if(startYear != null || endYear != null){
			Criteria criteria = Criteria.where("registerTime");
			if(startYear !=null )
				criteria.gte(startYear+"-01-01");
			if(endYear !=null )
				criteria.lte(endYear+"-12-31");
			query.addCriteria(criteria);
		}
		if(ids != null){
			query.addCriteria(Criteria.where("id").in(ids));
		}
		Criteria criteria = Criteria.where("status").gte(Car.STATUS_NEW);
		if(!Tool.isEmpty(shelves)){
			if(status != null){
				if(status != Car.STATUS_SHELVES && status != Car.STATUS_STORAGE){
					criteria = Criteria.where("status").is("");
				}
				else{
					criteria = Criteria.where("status").is(status);
				}
			}
			else{
				List<Integer> statuses = new ArrayList<Integer>();
				statuses.add(Car.STATUS_SHELVES);
				statuses.add(Car.STATUS_STORAGE);
				criteria.in((statuses));
			}
		}
		else if(status!=null){
			criteria = Criteria.where("status").is(status);
		}
		query.addCriteria(criteria);
		if(promo !=null ){
			query.addCriteria(Criteria.where("promo").is(promo));
		}
		if(!Tool.isEmpty(keywords)){
			keywords = keywords.replaceAll("　", " ");
			String[] keyword = keywords.split(" ");
			if(keyword != null && keyword.length>0){
				List<Criteria> criterias = new ArrayList<Criteria>(); 
				for(int i=0;i<keyword.length;i++){
					if(!Tool.isEmpty(keyword[i])){
						criterias.add(Criteria.where("keywords").regex(".*"+keyword[i].trim()+".*", "i"));
					}
				}
				if(criterias.size()>0)
					query.addCriteria(new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()])));
			}
		}
		if(pageMan.isPage()){
			Long k = this.getMongoTemplate().count(query, Car.class);
			pageMan.setTotalRow(k.intValue());
			query.skip(pageMan.getStartRecord()).limit(pageMan.getPageSize());
		}
		if(!Tool.isEmpty(sort)){
			if(sort.equals("inDate")){
				query.sort().on("status", Order.ASCENDING).on("inDate", Order.ASCENDING);
			}
			else{
				if(!Tool.isEmpty(sortType)&& sortType.equals("1"))
					query.sort().on(sort, Order.ASCENDING);
				else
					query.sort().on(sort, Order.DESCENDING);
			}
		}
		else{
			query.sort().on("status", Order.ASCENDING).on("shelvesTime", Order.DESCENDING);
		}
		query.fields().exclude("updateMan");
		query.sort().on("status", Order.ASCENDING).on("updateTime", Order.DESCENDING).on("_id", Order.ASCENDING);
		return this.getMongoTemplate().find(query, Car.class);
	}
	
	@Override
	public Car update(Car car){
		this.getMongoTemplate().save(car);
		return car;
	}
}
